package DomainLayer;
import java.util.ArrayList;

public class ToolStock
{
    private ArrayList<ArrayList<Tool>> stock;
    private static ToolStock instance;

    private ToolStock()
    {
        stock= new ArrayList<ArrayList<Tool>>();
    }
    
    //@return only one object of class ToolStock.
    public static ToolStock getInstance()
    {
        if(instance==null)
        {
            instance=new ToolStock();
        }
        return instance;
    }

    public void addTool(String title, String barCode, double pricePerDay, double deposit, int number)
    {
        ArrayList<Tool> list=new ArrayList<Tool>();
        for(int i=1; i<=number; i++)
        {
            list.add(new Tool(title,barCode,pricePerDay, deposit,i));
        }
        stock.add(list);
    }
    
    public ArrayList<Tool> searchTools(String barCode)
    {
        for(ArrayList<Tool> search : stock)
        {
            if(search.get(0).getBarCode().equals(barCode))
            {
                return search;
            }
        }
        return null;
    }
    
    
    public Tool searchTool(String barCode,int id)
    {
        ArrayList<Tool> toolList=searchTools(barCode);
        for( Tool tool : toolList)
        {
            if( tool.getID()==id)
            {
                return tool;
            }
            
        }
        return null;
    }
    
    public String getTool(String barCode)
    {
        return  searchTools(barCode).get(0).toStrings() + '\n' + " Number of items of tools of this type: " + searchTools(barCode).size()+ '\n';
    }
    
    public Tool searchAvailableTool(ArrayList<Tool> list)
    {
        for(Tool tool: list)
        {
            if(!tool.isLoaned())
            {
                return tool;
            }
        }
        return null;
        
    }
    
    public void deleteTool(String barCode, int id)
    {ArrayList<Tool> toolList=searchTools(barCode);
        for( Tool tool : toolList)
        {
            if( tool.getID()==id)
            {
                toolList.remove(tool);
                break;
            }
        
        }
        
    }
    
    public void removeSameTools(ArrayList<Tool> tools)
    {
        stock.remove(tools);
    }
    
    public void loanTool(Tool tool)
    {
        tool.loan(); 
    }
    
    public void unloanTool(Tool tool)
    {
        tool.unloan();
    }
}